/**
 * Created by wangshuyi on 2017/2/4.
 */

'use strict';

var productListPage = {
    init: null,
    initElement: null,
    initEvent: null,
};

productListPage.init = function () {
    this.initElement();
    this.initEvent();
};

productListPage.initElement = function () {
    var thisPage = this;
};
productListPage.initEvent = function () {
    var thisPage = this;
    var tempArr=[];

    var today = new Date();
    today.setHours(0);
    today.setMinutes(0);
    today.setSeconds(0);

    $('#unhandle').click(function () {
        $(this).addClass('weui-bar__item--on').siblings().removeClass('weui-bar__item--on');
        Dolphin.ajax({
            url:contextData.contextPath+'/work/getGiftInfo',
            type:"get",
            forceUrl: 'true',
            data:{cardCard: contextData.data.cardCode || contextData.body.cardCode,openId:contextData.userData.openid},
            onSuccess:function (data) {
                var con='';
                tempArr = [];
                if(data.lists.length!==0){
                    var list = data.lists;
                    $('.container').empty();
                    list.forEach(function(l){
                        if(new Date(l.endDate)<today){
                            tempArr.push(l)
                        }else{
                            var moneyFlag = l.giftAmt<1?false:true;

                            var block = $('<div class="gift-block-new">');
                            con = '        <div class="gift-block-new__price">\n';
                            if(moneyFlag){
                                con +='            <small>￥</small>\n' +
                                    '            <span>'+l.giftAmt+'</span>\n';
                            }else{
                                con +='            <span>'+(l.giftAmt*10)+'</span>\n' +
                                    '            <small>折</small>\n';
                            }
                            let build_type_name = l.build_type_name ? "("+l.build_type_name+")":'';
                            con += '        </div>\n' +
                                '        <div class="gift-block-new__info">\n' +
                                '            <h4>'+(moneyFlag?'现金券':'折扣券')+'</h4>\n' +
                                '            <div class="gift-block-new__info-primary">满'+l.limitAmt+'元可用</div>\n' +
                                '            <div>\n' +
                                '                <label>券　号：</label>' + l.giftCode + build_type_name +
                                '            </div>\n' +
                                '            <div>\n' +
                                '                <label>有效期：</label>'+l.beginDate+' 至 ' + l.endDate +
                                '            </div>\n' +
                                '        </div>';
                            block.html(con);

                            block.click(function(){
                                JsBarcode('#barCodeModel', l.giftCode, {});
                                $('#giftModalTitle').html(l.giftAmt<1?((l.giftAmt*10) + '折 折扣券'):('￥' + l.giftAmt + ' 现金券'));
                                $('#giftModalTitle').append("<span style='font-size: 10px;color: red;'>&nbsp;&nbsp;启用金额:"+ (l.limitAmt || 0)  + "元</span>");
                                $('#giftBarModal').show();
                            });
                            $('.container').append(block);
                        }
                    });

                    $('#outNum').html(tempArr.length);
                    $('#unhandleNum').html(list.length - tempArr.length);
                }
                else{
                    $('#unhandleNum').html(0);
                    $('#outNum').html(0);
                    $('.container').html( '暂无任何优惠券');

                }
            },
            onError:function (e) {
                console.log(e);
            }
        });
    });
    $('#timeOut').click(function () {
        $(this).addClass('weui-bar__item--on').siblings().removeClass('weui-bar__item--on');
        var con='';
        if(tempArr.length!=0){
            var list = tempArr;
            for(var i=0;i<tempArr.length;i++){
                var moneyFlag = list[i].giftAmt<1?false:true;
                con += '<div class="gift-block-new gift-block-new-disabled">\n' +
                    '        <div class="gift-block-new__price">\n';
                if(moneyFlag){
                    con +='            <small>￥</small>\n' +
                        '            <span>'+list[i].giftAmt+'</span>\n';
                }else{
                    con +='            <span>'+(list[i].giftAmt*10)+'</span>\n' +
                        '            <small>折</small>\n';
                }
                let build_type_name = list.build_type_name ? "("+list.build_type_name+")":'';
                con += '        </div>\n' +
                    '        <div class="gift-block-new__info">\n' +
                    '            <h4>'+(moneyFlag?'现金券':'折扣券')+'</h4>\n' +
                    '            <div class="gift-block-new__info-primary">满'+list[i].limitAmt+'元可用</div>\n' +
                    '            <div>\n' +
                    '                <label>券　号：</label>' + list[i].giftCode + build_type_name +
                    '            </div>\n' +
                    '            <div>\n' +
                    '                <label>有效期：</label>'+list[i].beginDate+' 至 ' + list[i].endDate +
                    '            </div>\n' +
                    '        </div>\n' +
                    '        <img class="gift-disabled-icon" src="'+Dolphin.path.publicPath+'/images/gift-disabled-icon.png" />\n' +
                    '    </div>';

            }
            $('.container').html(con);
        }
        else {
            $('.container').html('暂无过期优惠券');
        }
    });
    $('.calendar').click(function () {
        $('.searchBtn').css("background","transparent");
    });
    $('.searchBtn').click(function () {
        $(this).addClass('weui-bar__item--on').siblings().removeClass('weui-bar__item--on');
        $(this).css({"background":"red"}).siblings().css("background","transparent");
    });
    $('.gift-bar-modal__panel-close').click(function(){
        $('#giftBarModal').hide();
    })

    Dolphin.ajax({
        url:contextData.contextPath+'/work/getGiftInfo',
        type:"get",
        forceUrl: 'true',
        data:{cardCard: contextData.data.cardCode || contextData.body.cardCode,openId:contextData.userData.openid},
        onSuccess:function (data) {
            var con='';
            if(data.lists.length!=0){
                var list = data.lists;
                $('.container').empty();
                list.forEach(function(l){
                    if(new Date(l.endDate)<today){
                        tempArr.push(l)
                    }else{
                        var moneyFlag = l.giftAmt<1?false:true;

                        var block = $('<div class="gift-block-new">');
                        con = '        <div class="gift-block-new__price">\n';
                        if(moneyFlag){
                            con +='            <small>￥</small>\n' +
                                '            <span>'+l.giftAmt+'</span>\n';
                        }else{
                            con +='            <span>'+(l.giftAmt*10)+'</span>\n' +
                                '            <small>折</small>\n';
                        }
                        let build_type_name = l.build_type_name ? " ("+l.build_type_name+")":'';
                        con += '        </div>\n' +
                            '        <div class="gift-block-new__info">\n' +
                            '            <h4>'+(moneyFlag?'现金券':'折扣券')+'</h4>\n' +
                            '            <div class="gift-block-new__info-primary">满'+l.limitAmt+'元可用</div>\n' +
                            '            <div>\n' +
                            '                <label>券　号：</label>' + l.giftCode + build_type_name +
                            '            </div>\n' +
                            '            <div>\n' +
                            '                <label>有效期：</label>'+l.beginDate+' 至 ' + l.endDate +
                            '            </div>\n' +
                            '        </div>';
                        block.html(con);

                        block.click(function(){
                            JsBarcode('#barCodeModel', l.giftCode, {});
                            $('#giftModalTitle').html(l.giftAmt<1?((l.giftAmt*10) + '折 折扣券'):('￥' + l.giftAmt + ' 现金券'));
                            $('#giftModalTitle').append("<span style='font-size: 10px;color: red;'>&nbsp;&nbsp;启用金额:"+ (l.limitAmt || 0) + "元</span>");
                            $('#giftBarModal').show();
                        });
                        $('.container').append(block);
                    }
                });

                $('#outNum').html(tempArr.length);
                $('#unhandleNum').html(list.length - tempArr.length);
            }
            else{
                $('#unhandleNum').html(0);
                $('#outNum').html(0);
                $('.container').html( '暂无任何优惠')
            }
        },
        onError:function (e) {
            alert('网络有些差，请重新打开吧')
        }
    });
};

$(function () {
    productListPage.init();
});
